import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/common/no_permissions_widget.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/file_util.dart';
import 'package:safetymonitor/app/data/entities/application/security_control_promise_entity.dart';
import 'controller.dart';

///风险承诺
class RiskCommitmentPage extends StatelessWidget {
  RiskCommitmentPage({super.key});

  final controller = Get.put(RiskCommitmentController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          if (!controller.isNotPermissions) {
            return const NoPermissionsWidget();
          }
          return Scaffold(
            backgroundColor: AppColors.bg_page_color,
            appBar: CustomAppBar(
              titleText: '风险承诺',
              actions: [
                //新增
                IconButton(
                  icon: const Icon(Icons.add),
                  onPressed: controller.addRiskSecurityPromise,
                ),
              ],
            ),
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: Visibility(
                            visible: controller.currentMainCompany ==
                                controller.userCompanyName,
                            child: _company()),
                      ),
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child:
                          CustomEasyRefreshList<SecurityControlPromiseEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.getRiskSecurityPromise,
                    itemBuilder: (item, index) {
                      return _itemWidget(item);
                    },
                  ))
                ],
              ),
            ),
          );
        });
  }

  /// 标题
  Widget _title(String title, String? value,
      {Color color = const Color(0xff495061)}) {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
            color: const Color(0xffA5A6B5),
            fontSize: 13.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
        if (value != null)
          Expanded(
              child: Text(
            value,
            style: TextStyle(
              color: color,
              fontSize: 13.sp,
              fontWeight: FontWeight.w400,
            ),
          )),
      ],
    );
  }

  Widget _itemWidget(SecurityControlPromiseEntity item) {
    return CustomTitleChartPage(
        title:'上传人:${item.uploader}',
        child: Column(
          children: [
            _title(
              '上传时间：',
              item.uploadTime!=0?Jiffy.parseFromMillisecondsSinceEpoch(item.uploadTime)
                  .format(pattern: 'yyyy-MM-dd HH:mm:ss'):'',
            ),
            _title('文件：', '查看文件', color: Colors.blueAccent).clickOnce(() {
              //打开文件
              FileUtils.openFile(item.path);
            }),
          ],
        ));
  }

  ///公司
  Widget _company() {
    return Container(
      // width: 120.0,
      height: 30.0,
      // margin: const EdgeInsets.only(right: 10),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: const Color(0xffDCE1EA)),
          borderRadius: BorderRadius.circular(6)),
      child: Row(
        children: [
          const SizedBox(
            width: 12,
          ),
          Expanded(
              child: Text(
            controller.getString(),
            // controller.selectCompany.isNotEmpty? controller.selectCompany:'公司名',
            style: TextStyles.grey_14,
            maxLines: 1,
          )),
          const Icon(
            Icons.keyboard_arrow_down,
            color: AppColors.font_color_gray,
          ),
          const SizedBox(
            width: 8,
          ),
        ],
      ),
    ).clickOnce(controller.selectCompanyOn);
  }
}
